package com.action;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.struts2.ServletActionContext;

import com.entity.FileEntity;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class downloadFileAction extends ActionSupport implements ModelDriven<FileEntity>{
	private static final long serialVersionUID = 1L;
	//模型驱动使用的对象
	private FileEntity fileEntity = new FileEntity();
	    public FileEntity getFileEntity() {
		return fileEntity;
	}
	public void setFileEntity(FileEntity fileEntity) {
		this.fileEntity = fileEntity;
	}
		private FileEntity userArticle = new FileEntity();
	    @Override
	    public FileEntity getModel() {
	        return userArticle;
	    }
	private String ContentType;
	        private InputStream fileStream;

	        private String docName;
	        public String getDocName() throws UnsupportedEncodingException {

	            docName = fileEntity.getFileName();
	            docName = URLEncoder.encode(docName,"UTF-8");//解决文件名中文显示不出或者乱码问题
	            return docName;
	        }
	        public InputStream getFileStream() throws UnsupportedEncodingException {
	            return fileStream;
	        }
	        public void setFileStream(InputStream fileStream) {
	            this.fileStream = fileStream;
	        }

	        public String download(){
	            System.out.println("filename:"+fileEntity.getFileName());
	            fileStream = ServletActionContext.getServletContext().getResourceAsStream("/upload/"+fileEntity.getFileName());          
	            return "success";
	        }
	}
